﻿@using Website.CRUDServices
@using Location = Website.Models.Domain.Location
@using Profile = Website.CRUDServices.Profile
@using Region = Website.Models.Domain.Region
@{
    this.ViewBag.Title = "Klynge";
    this.Layout = "~/Views/_Shared/_Layout.cshtml";
    var Regions = this.ViewData["Regions"] as List<Region>;
    var Locations = this.ViewData["Locations"] as List<Location>;
    var Profiles = this.ViewData["Profiles"] as List<Profile>;
    var ClusterStatusTypes = this.ViewData["ClusterStatusTypes"] as List<ClusterStatusType>;
    var ClusterMethodTypes = this.ViewData["ClusterMethodTypes"] as List<ClusterMethodType>;
    var Projects = this.ViewData["Projects"] as List<Project>;
    var errorMsg = new HtmlString(string.Empty);
    int counter = 0;
    var Cluster = Model as Cluster;
    var ClusterMethods = this.ViewData["ClusterMethods"] as List<ClusterMethod>;
    var ClusterStatuses = this.ViewData["ClusterStatuses"] as List<ClusterStatus>;
    var clusterStatusTypes = ClusterStatuses.Select(x => x.Type).ToList();
    var clusterMethodTypes = ClusterMethods.Select(x => x.Type).ToList();
    var filesInfo = this.ViewData["FilesInfo"] as List<Website.CRUDServices.File> ?? new List<Website.CRUDServices.File>();
    if (this.ViewData["ErrorMessage"] != null)
    {
        errorMsg = this.ViewData["ErrorMessage"] as HtmlString;
    }

}

@Html.Partial("~/Views/_Shared/LogView.cshtml", Cluster.ID)


<script type="text/javascript">
    //toggle script toggles div with id = id
    function toggle(id) {
        if (document.getElementById(id).style.display == 'none' || document.getElementById(id).style.display == "") {
            document.getElementById(id).style.display = 'block';
        } else {
            document.getElementById(id).style.display = 'none';
        }
    }
</script>

@*// *******************Cluster**************************@

@*// name*@
<fieldset class="chosenFields">
    <legend>Navn på klynge</legend>
    @Cluster.Name
</fieldset>

@*// date*@
<fieldset class="chosenFields">
    <legend>Dato</legend>
    @if(Cluster.Date.HasValue){
    @Cluster.Date.Value.ToShortDateString()
    }
</fieldset>

@*// location*@
<fieldset class="chosenFields">
    <legend>Lokation</legend>

    @foreach (Location loc in Locations.OrderBy(x => x.Name))
    {
        if (loc.ID == Cluster.Location)
        {
        @loc.Name <br />
        }
    }
</fieldset>

@*// national or international?*@
<fieldset class="chosenFields">
    <legend>Hvad er projektet?</legend>

    <input type="radio" name="IsNational" value="National"@if (Cluster.IsNational)
                                                          {@Html.Raw("checked")}>Nationalt
        <input type="radio" name="IsNational" value="International"@if (!Cluster.IsNational)
                                                                   {@Html.Raw("checked")}>Internationalt
</fieldset>

@*// ownerProjects chooser*@
<fieldset class="chosenFields">
    <legend>Alle projekter tilknyttet</legend>

    @foreach (Project proj in Projects.Where(x => (Cluster.OwnerProjects ?? new Guid[0]).Contains(x.ID)))
    {
        @proj.Name <br />
    }

</fieldset>

@*// profiles chooser*@
<fieldset class="chosenFields">
    <legend>Profiler</legend>

    @foreach (Profile prof in Profiles.Where(x => (Cluster.Profiles ?? new Guid[0]).Contains(x.ID)))
    {
        @prof.Name<br />
    }
</fieldset>

@*// *******************ClusterStatus**************************@
<div class="formBullet">Status</div>

<br />
@foreach (ClusterStatusType cst in ClusterStatusTypes)
{
   
    <fieldset class="chosenFields">
        <legend>@cst.Name</legend>
        @if (clusterStatusTypes.Contains(cst.ID))
        {@Html.Raw("<script>$(function () {toggle('" + cst.ID + "');});</script>")}
        <div class="formEntry open_hidden" id="@cst.ID">

            <!--active?-->
            <div class="formEntry">
                <input type="radio" name="ClusterStatus[@counter].IsActive" value="true"@if (ClusterStatuses.Where(x => x.Type == cst.ID).Any() && ClusterStatuses.Single(x => x.Type == cst.ID).IsActive)
                                                                                        {@Html.Raw("checked")}>Aktiv
                <input type="radio" name="ClusterStatus[@counter].IsActive" value="false" @if (ClusterStatuses.Where(x => x.Type == cst.ID).Any() && !ClusterStatuses.Single(x => x.Type == cst.ID).IsActive)
                                                                                          {@Html.Raw("checked")}>Inaktiv
            </div>
            <!--comments-->
            <fieldset class="chosenFields">
                <legend>Kommentarer  til @cst.Name</legend>

                @if (ClusterStatuses.Any(x => x.Type == cst.ID))
                {
                    @ClusterStatuses.Single(x => x.Type == cst.ID).Comments
                }
            </fieldset>

        </div>
    </fieldset>
                counter++;
}

@*// *******************ClusterMethod**************************@
<div class="formBullet">Metode</div>
<br />

@foreach (ClusterMethodType cmt in ClusterMethodTypes)
{
    <fieldset class="chosenFields">
        <legend>@cmt.Name</legend>
        @if (clusterMethodTypes.Contains(cmt.ID))
        {@Html.Raw("<script>$(function () {toggle('" + cmt.ID + "');});</script>")}
        <div class="formEntry open_hidden" id="@cmt.ID">

            <!--comments-->
            <fieldset class="chosenFields">
                <legend>Kommentarer til @cmt.Name</legend>
                @if (ClusterMethods.Any(x => x.Type == cmt.ID))
                {
                    @ClusterMethods.Single(x => x.Type == cmt.ID).Comments
                }
            </fieldset>

            <fieldset class="chosenFields">
                <legend>Eksisterende filer</legend>

                <script type="text/javascript">
                    function openFile(id) {
                        var url = "@Url.Action("Open", "File", new { ID = ".id." })";
                        window.open(url.replace(".id.", id), "blank_" + id);
                    }
                </script>
                <table class="fileTable" id="FileTable">
                    @if (ClusterMethods.Any(x => x.Type == cmt.ID))
                    {
                        var fileIDs = ClusterMethods.Single(x => x.Type == cmt.ID).Files;

                        foreach (var file in filesInfo.Where(x => fileIDs.Contains(x.ID)))
                        {
                        <tr id="@Html.Raw("tr_" + file.ID)">
                            <td>@file.Name</td>
                            <td>
                                <input type="button" value="Se fil" onclick="@Html.Raw("javascript:openFile('" + file.ID + "');")" />
                            </td>
                        </tr>
                        }
                    }
                </table>
            </fieldset>

            <!--arkchive location-->
            <fieldset class="chosenFields">
                <legend>Fysisk arkiverings placering</legend>
                @if (ClusterMethods.Any(x => x.Type == cmt.ID))
                {
                    @ClusterMethods.Single(x => x.Type == cmt.ID).ArchiveLocation
                }
            </fieldset>
        </div>
    </fieldset>
}

